package com.uaroadshow.entity.trade;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 *
 * @author Ark
 *
 */
@Entity
@Table(name = "product_stat")
@JsonIgnoreProperties(value = { "hibernate_lazy_initializer", "handler", "fieldHandler" })
public class ProductStat {

    @Id
    @GeneratedValue
    private Long id;
    
    /** 对应课程 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private Product product;
    
    /** 库存总计 */
    private int totals;
    
    /** 剩余库存 */
    private int rests;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public ProductStat() {
    }

    /**
     * @return
     */
    public boolean noRest() {
        return this.rests <= 0;
    }
    
    /**
     * 库存减少
     * 
     * @return
     */
    public ProductStat reduce() {
        this.rests--;
        return this;
    }
    
    /**
     * @param rests
     * @return
     */
    public ProductStat editRest(int rests) {
        this.rests = rests;
        return this;
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the product
     */
    public Product getProduct() {
        return product;
    }

    /**
     * @return the totals
     */
    public int getTotals() {
        return totals;
    }

    /**
     * @return the rests
     */
    public int getRests() {
        return rests;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(Product product) {
        this.product = product;
    }

    /**
     * @param totals the totals to set
     */
    public void setTotals(int totals) {
        this.totals = totals;
    }

    /**
     * @param rests the rests to set
     */
    public void setRests(int rests) {
        this.rests = rests;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
}
